package com.netwit.shiro.jwt.token.service.impl;

import com.netwit.shiro.jwt.redis.RedisUtil;
import com.netwit.shiro.jwt.token.service.SecretService;
import com.netwit.shiro.jwt.token.service.TokenService;
import com.netwit.shiro.jwt.util.JWTUtil;
import com.netwit.shiro.jwt.util.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/token/api")
@Slf4j
public class TokenServiceImpl implements TokenService {
    @Autowired
    private SecretService secretService;

    @Autowired
    private RedisTemplate<String, String> template;

    @Override
    public ResponseBean checkToken(@RequestParam("token") String token) {
        String userId = JWTUtil.getUserId(token);
        String secret = secretService.secret(userId);
        String flag   = JWTUtil.verify(token, userId, secret);
        if (RedisUtil.get(template, token) == null) {
            return new ResponseBean(200, "RedisTokenExpired", null);
        } else {
            return new ResponseBean(200, flag, null);
        }
    }
}
